package com.lxq.test1812.Main02;

import java.util.Scanner;

class State{
    int type;
    int time;

    public State(int type, int time) {
        this.type = type;
        this.time = time;
    }

}

public class Main {
    public static int[] lightState = new int[3];

    public static State nowState(long sumTime,State state){
//        long lastTime = sumTime - state.time;
//        while (lastTime>=0){
//            state.type--;
//            if (state.type==0) state.type=3;
//            lastTime = lastTime - lightState[state.type-1];
//        }
        int[] a = {0,lightState[0],lightState[0]+lightState[2]+lightState[1],lightState[0]+lightState[2]};
        int lastTime = Math.toIntExact((a[state.type] - state.time + sumTime) % (lightState[0] + lightState[2] + lightState[1]));
        if (lastTime<lightState[0]) {
            state.time = lightState[0]-lastTime;
            state.type = 1;
        }
        else if (lastTime<lightState[0]+lightState[2]) {
            state.time = lightState[0]+lightState[2]-lastTime;
            state.type = 3;
        }
        else{
            state.time = lightState[0]+lightState[1]+lightState[2]-lastTime;
            state.type = 2;
        }
        return state;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        lightState[0] = sc.nextInt();//r 红
        lightState[1] = sc.nextInt();//y 黄
        lightState[2] = sc.nextInt();//g 绿

        State state = new State(0,0);
        long sumTime = 0; //总时间
        int n = sc.nextInt();

        for (int i=0;i<n;i++){
            state.type = sc.nextInt();
            state.time = sc.nextInt();
            //红灯黄灯绿灯都先转换状态
            switch (state.type){
                case 1:
                case 2:
                case 3:{
                    state = nowState(sumTime,state);
                    break;
                }
            }
            //根据状态做操作
            switch (state.type){
                case 0:{
                    sumTime = sumTime + state.time;
                    break;
                }
                case 1:{
                    sumTime = sumTime + state.time;
                    break;
                }
                case 2:{
                    sumTime = sumTime + state.time + lightState[0];
                    break;
                }
                case 3:break;
            }
        }

        System.out.println(sumTime);


    }
}
